<template>
  <div class="contentCate">
    <el-card class="box-card" v-if="status === 'index'">
      <el-row>
        <el-select
          v-model="searchParams.type"
          placeholder="请选择类型"
          clearable
          size="mini"
        >
          <el-option
            v-for="item in typeArr"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          ></el-option>
        </el-select>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getData"
          v-if="has('tasks/index')"
        >搜索
        </el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="add"
          v-if="has('tasks/store')"
        >添加
        </el-button
        >
      </el-row>
      <el-table
        :data="data.data"
        v-loading="table_loading"
        row-key="id"
        style="width: 100%;margin:10px auto"
        size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }"
      >
        <el-table-column
          fixed
          prop="id"
          label="ID"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          align="center"
        ></el-table-column>
        <el-table-column prop="type" label="类型" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.type == 1" style="color:red">每日任务</span>
            <span v-if="scope.row.type == 2" style="color:green">每周任务</span>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="任务类型" align="center">
          <template slot-scope="scope">
            <!--            <span v-if="scope.row.reach_type == 1" style="color:red">擂台赛</span>-->
            <!--            <span v-if="scope.row.reach_type == 2" style="color:green">签到</span>-->
            <!--            <span v-if="scope.row.reach_type == 3" style="color:red">拳王赏或空空赏</span>-->
            <!--            <span v-if="scope.row.reach_type == 4" style="color:green">开启宝箱</span>-->
            <!--            <span v-if="scope.row.reach_type == 5" style="color:red">首次分享</span>-->
            <!--            <span v-if="scope.row.reach_type == 6" style="color:green">天选或一番</span>-->
            <!--            <span v-if="scope.row.reach_type == 7" style="color:red">累计消费</span>-->
            <!--            <span v-if="scope.row.reach_type == 8" style="color:green">超神或天选赏SP</span>-->
            <!--            <span v-if="scope.row.reach_type == 9" style="color:red">无限赏</span>-->
            <!--            <span v-if="scope.row.reach_type == 10" style="color:green">天选赏</span>-->
            <!--            <span v-if="scope.row.reach_type == 11" style="color:red">无限赏超神</span>-->
            <!--            <span v-if="scope.row.reach_type == 12" style="color:green">完成所有</span>-->
            <span v-if="scope.row.reach_type == 13" style="color:green">流水</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="reach_num"
          label="任务数量"
          align="center"
        ></el-table-column>
        <el-table-column prop="type" label="奖励类型" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.award_type == 1" style="color:red">金币</span>
            <span v-if="scope.row.award_type == 2" style="color:green">钻石</span>
            <span v-if="scope.row.award_type == 3" style="color:green">日转盘次数</span>
            <span v-if="scope.row.award_type == 4" style="color:green">周转盘次数</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="award_num"
          label="奖励（个）"
          align="center"
        ></el-table-column>

        <el-table-column fixed="right" label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="edit(scope.row.id)"
              v-if="has('tasks/show')"
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="del(scope.row.id, scope.$index)"
              v-if="has('tasks/destroy')"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="text-align:center;margin:20px 0;"
        :current-page="searchParams.page"
        :page-sizes="pageSizes"
        layout="total,prev, pager, next,sizes"
        :total="data.total"
        @size-change="changeSize"
        @current-change="changePage"
      ></el-pagination>
    </el-card>

    <el-card class="box-card" v-if="status === 'add'">
      <div slot="header">
        <el-page-header @back="back" content="添加"></el-page-header>
      </div>
      <el-form
        :model="editParams"
        :rules="rules"
        ref="form"
        label-width="120px"
      >
        <el-form-item label="类型" prop="type">
          <el-select v-model="editParams.type">
            <el-option
              v-for="item in typeArr"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="amount" style="width:300px">
          <el-input
            placeholder="请输入标题"
            v-model="editParams.title"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="任务类型" prop="reach_type">
          <el-select v-model="editParams.reach_type">
            <el-option
              v-for="item in reachType"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务数量" prop="reach_num" style="width:300px">
          <el-input
            placeholder="请输入任务数量"
            v-model="editParams.reach_num"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="奖励类型" prop="award_type">
          <el-select v-model="editParams.award_type">
            <el-option
              v-for="item in awardType"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="奖励数量" prop="amount" style="width:300px">
          <el-input
            placeholder="请输入奖励数量"
            v-model="editParams.award_num"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="editParams.status">
            <el-option
              v-for="item in statusArr"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <!--        <el-form-item-->
        <!--          label="奖励"-->
        <!--          v-for="(reward, key) in editParams.task_reward"-->
        <!--          :key="reward.id"-->
        <!--        >-->
        <!--          <el-form-item label="类型">-->
        <!--            <el-select v-model="reward.type">-->
        <!--              <el-option-->
        <!--                v-for="v in rewardTypeArr"-->
        <!--                :key="v.id"-->
        <!--                :label="v.title"-->
        <!--                :value="v.id"-->
        <!--              ></el-option>-->
        <!--            </el-select>-->
        <!--            <el-button type="primary" size="mini" @click="removeReward(key)"-->
        <!--            >删除奖励-->
        <!--            </el-button-->
        <!--            >-->
        <!--          </el-form-item>-->
        <!--          <el-form-item label="优惠券" v-if="reward.type == 2">-->
        <!--            <el-select v-model="reward.ext_id">-->
        <!--              <el-option-->
        <!--                v-for="v in couponData"-->
        <!--                :key="v.id"-->
        <!--                :label="v.name"-->
        <!--                :value="v.id"-->
        <!--              ></el-option>-->
        <!--            </el-select>-->
        <!--          </el-form-item>-->

        <!--          <el-form-item label="反悔卡" v-if="reward.type == 3">-->
        <!--            <el-select v-model="reward.ext_id">-->
        <!--              <el-option-->
        <!--                v-for="v in backCardData"-->
        <!--                :key="v.id"-->
        <!--                :label="v.name"-->
        <!--                :value="v.id"-->
        <!--              ></el-option>-->
        <!--            </el-select>-->
        <!--          </el-form-item>-->
        <!--          <el-form-item label="数量">-->
        <!--            <el-input-->
        <!--              placeholder="请输入金额"-->
        <!--              v-model="reward.num"-->
        <!--              autocomplete="off"-->
        <!--            ></el-input>-->
        <!--          </el-form-item>-->
        <!--        </el-form-item>-->

        <el-form-item>
          <!--          <el-button type="primary" @click="addReward()">添加奖励</el-button>-->
          <el-button
            type="primary"
            @click="submit('form', 1)"
            v-if="has('tasks/store')"
          >确 定
          </el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card" v-if="status === 'edit'">
      <div slot="header">
        <el-page-header @back="back" content="编辑"></el-page-header>
      </div>
      <el-form
        :model="editParams"
        :rules="rules"
        ref="form"
        label-width="120px"
      >
        <el-form-item label="类型" prop="type">
          <el-select v-model="editParams.type">
            <el-option
              v-for="item in typeArr"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="amount" style="width:300px">
          <el-input
            placeholder="请输入标题"
            v-model="editParams.title"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="任务类型" prop="reach_type">
          <el-select v-model="editParams.reach_type">
            <el-option
              v-for="item in reachType"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务数量" prop="reach_num" style="width:300px">
          <el-input
            placeholder="请输入任务数量"
            v-model="editParams.reach_num"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="奖励类型" prop="award_type">
          <el-select v-model="editParams.award_type">
            <el-option
              v-for="item in awardType"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="奖励数量" prop="amount" style="width:300px">
          <el-input
            placeholder="请输入奖励数量"
            v-model="editParams.award_num"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="editParams.status">
            <el-option
              v-for="item in statusArr"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <!--        <el-form-item-->
        <!--          label="奖励"-->
        <!--          v-for="(reward, key) in editParams.task_reward"-->
        <!--          :key="reward.id"-->
        <!--        >-->
        <!--          <el-form-item label="类型">-->
        <!--            <el-select v-model="reward.type">-->
        <!--              <el-option-->
        <!--                v-for="v in rewardTypeArr"-->
        <!--                :key="v.id"-->
        <!--                :label="v.title"-->
        <!--                :value="v.id"-->
        <!--              ></el-option>-->
        <!--            </el-select>-->
        <!--            <el-button type="primary" size="mini" @click="removeReward(key)"-->
        <!--            >删除奖励-->
        <!--            </el-button-->
        <!--            >-->
        <!--          </el-form-item>-->
        <!--          <el-form-item label="优惠券" v-if="reward.type == 2">-->
        <!--            <el-select v-model="reward.ext_id">-->
        <!--              <el-option-->
        <!--                v-for="v in couponData"-->
        <!--                :key="v.id"-->
        <!--                :label="v.name"-->
        <!--                :value="v.id"-->
        <!--              ></el-option>-->
        <!--            </el-select>-->
        <!--          </el-form-item>-->

        <!--          <el-form-item label="反悔卡" v-if="reward.type == 3">-->
        <!--            <el-select v-model="reward.ext_id">-->
        <!--              <el-option-->
        <!--                v-for="v in backCardData"-->
        <!--                :key="v.id"-->
        <!--                :label="v.name"-->
        <!--                :value="v.id"-->
        <!--              ></el-option>-->
        <!--            </el-select>-->
        <!--          </el-form-item>-->
        <!--          <el-form-item label="数量">-->
        <!--            <el-input-->
        <!--              placeholder="请输入金额"-->
        <!--              v-model="reward.num"-->
        <!--              autocomplete="off"-->
        <!--            ></el-input>-->
        <!--          </el-form-item>-->
        <!--        </el-form-item>-->

        <el-form-item>
          <!--          <el-button type="primary" @click="addReward()">添加奖励</el-button>-->
          <el-button
            type="primary"
            @click="submit('form', 2)"
            v-if="has('tasks/update')"
          >确 定
          </el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import api from "../../api/api.js";

export default {
  name: "contentCate",
  data() {
    return {
      status: "index", // 页面控制
      data: [], // 表格诗句
      table_loading: true, // 表格加载中
      loading: false, //全局加载中

      pageSizes: [10, 20, 50, 100, 200, 500, 800, 1000, 2000], // 每页条数默认10
      searchParams: {
        // 搜索参数
        page: 1,
        limit: 10,
        type: ""
      },
      editParams: {
        type: "",
        num: "",
        status: "",
        task_reward: []
      },
      activeValue: 1,
      inactiveValue: 0,
      rules: {
        title: [{required: true, message: "请输入标题", trigger: "blur"}],
        status: [{required: true, message: "请选择状态", trigger: "blur"}],
        type: [{required: true, message: "请选择类型", trigger: "blur"}]
      },
      statusArr: [
        {id: 1, title: "显示"},
        {id: 2, title: "隐藏"}
      ],
      typeArr: [
        {id: 1, title: "每日任务"},
        {id: 2, title: "每周任务"}
      ],
      awardType:[
        {id: 1, title: "金币"},
        {id: 2, title: "钻石"},
        {id: 3, title: "日转盘次数"},
        {id: 4, title: "周转盘次数"},
      ],
      reachType:[
        // {id: 1, title: "擂台赛"},
        // {id: 2, title: "签到"},
        // {id: 3, title: "拳王赏或空空赏"},
        // {id: 4, title: "开启宝箱"},
        // {id: 5, title: "首次分享"},
        // {id: 6, title: "天选或一番"},
        // {id: 7, title: "累计消费"},
        // {id: 8, title: "超神或天选赏SP"},
        // {id: 9, title: "无限赏"},
        // {id: 10, title: "天选赏"},
        // {id: 11, title: "无限赏超神"},
        {id: 13, title: "流水"},
      ],
      rewardTypeArr: [
        {id: 1, title: "钻石"},
        {id: 2, title: "优惠券"},
        {id: 3, title: "反悔卡"}
      ],
      couponData: [],
      backCardData: []
    };
  },
  created() {
    this.getData();
    this.getCoupon();
    this.getBackCard();
  },

  methods: {
    // 加载中
    load() {
      this.loading = this.$loading({
        lock: true,
        text: "数据加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
    },
    // 取消加载
    unload() {
      this.loading.close();
    },
    // 获取所有数据
    getData() {
      this.table_loading = true;
      api.tasks
        .index({params: this.searchParams})
        .then(res => {
          if (res.data.code == 200) {
            this.table_loading = false;
            this.data = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    addReward() {
      this.editParams.task_reward.push({id: "", type: "", num: ""});
    },
    removeReward(key) {
      this.editParams.task_reward.splice(key, 1);
    },
    changeStatus(e) {
      this.load();
      api.tasks
        .del(e.id)
        .then(res => {
          this.unload();
          if (res.data.code === 200) {
            this.$message.success("保存成功");
            this.init();
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 获取所有数据
    getCoupon() {
      api.common
        .getCoupon()
        .then(res => {
          if (res.data.code == 200) {
            this.couponData = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    getBackCard() {
      api.common
        .getBackCard()
        .then(res => {
          if (res.data.code == 200) {
            this.backCardData = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 重置搜索参数重新搜索
    reset() {
      this.init();
      this.getData();
    },
    back() {
      this.status = "index";
    },
    // 初始化
    init() {
      this.editParams = {
        type: "",
        num: "",
        status: "",
        task_reward: []
      };
      this.status = "index";
    },
    // 改变每页条数时
    changeSize(limit) {
      this.searchParams.limit = limit;
      this.getData();
    },
    // 改变当前页码时
    changePage(page) {
      this.searchParams.page = page;
      this.getData();
    },
    // 添加页面
    add() {
      this.status = "add";
    },
    // 编辑页面
    edit(id) {
      this.load();
      api.tasks
        .show(id)
        .then(res => {
          this.unload();
          if (res.data.code == 200) {
            this.status = "edit";
            this.editParams = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 保存添加或编辑
    submit(formName, type) {
      this.$refs[formName].validate((valid, error) => {
        if (valid) {
          // if (this.editParams.task_reward.length == 0) {
          //   this.$message.error("请填写奖励");
          //   return;
          // }
          // for (var i = 0; i < this.editParams.task_reward.length; i++) {
          //   if (!this.editParams.task_reward[i].num) {
          //     this.$message.error("请输入数量");
          //     return;
          //   }
          //   if (!this.editParams.task_reward[i].type) {
          //     this.$message.error("请选中奖励类型");
          //     return;
          //   }
          //   if (
          //     this.editParams.task_reward[i].type == 2 &&
          //     !this.editParams.task_reward[i].ext_id
          //   ) {
          //     this.$message.error("请选中优惠券");
          //     return;
          //   }
          //   if (
          //     this.editParams.task_reward[i].type == 3 &&
          //     !this.editParams.task_reward[i].ext_id
          //   ) {
          //     this.$message.error("请选中反悔卡");
          //     return;
          //   }
          // }
          this.load();

          // 添加
          if (type === 1) {
            api.tasks
              .store(this.editParams)
              .then(res => {
                this.unload();
                if (res.data.code == 200) {
                  this.reset();
                }
              })
              .catch(error => {
                console.log(error);
              });
          } else if (type == 2) {
            // 编辑
            api.tasks
              .edit(this.editParams.id, this.editParams)
              .then(res => {
                this.unload();
                if (res.data.code == 200) {
                  this.reset();
                }
              })
              .catch(error => {
                console.log(error);
              });
          }
        } else {
          this.$message.error(Object.values(error)[0][0].message);
          return false;
        }
      });
    },
    // 删除
    del(id, index) {
      this.$confirm("此操作将删除快递, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      })
        .then(() => {
          api.tasks
            .del(id)
            .then(res => {
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.data.data.splice(index, 1);
                this.data.total--;
              }
            })
            .catch(error => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped lang="less">
.contentCate {
  margin: 0 auto;

  .search {
    margin: 0 auto 10px auto;
  }
}
</style>
